package cn.trip.auth.service.impl;

import cn.itrip.beans.dto.Dto;
import cn.itrip.beans.pojo.ItripUser;
import cn.itrip.beans.vo.TokenVO;
import cn.itrip.common.RedisUtil;
import cn.itrip.common.TokenUtil;
import cn.itrip.dao.ItripUserMapper;
import cn.trip.auth.service.ItripUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("itripUserService")
public class ItripUserServiceImpl implements ItripUserService {
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private TokenUtil tokenUtil;
    @Autowired
    private ItripUserMapper itripUserMapper;


    /**
     * 用户登陆
     *
     * @param itripUser 用户对象，包含用户名和密码
     *                  redis命名为login:+用户账号
     * @return
     */
    @Override
    public Dto<ItripUser> selectByUserCode(ItripUser itripUser, String token) {
        ItripUser user1 = null;
        Dto dto = new Dto();
        if (token != null && token.equals("")) {
            if (redisUtil.hasKey(token)) {
                user1 = (ItripUser) redisUtil.get(token);
            } else {
                user1 = itripUserMapper.selectByUserCode(itripUser);
                token = tokenUtil.createToken(user1);
            }
        } else {
            user1 = itripUserMapper.selectByUserCode(itripUser);
            token = tokenUtil.createToken(user1);
        }
        if (user1 != null && user1.getUserPassword().equals(itripUser.getUserPassword())) {
            dto.setSuccess("true");
            dto.setErrorCode("0");
            TokenVO tokenVO = new TokenVO();
            tokenVO.setExpTime(36000L);
            tokenVO.setToken(token);
            dto.setData(tokenVO);
            redisUtil.set(token, user1, 36000L);
        } else if (user1 != null && !user1.getUserPassword().equals(itripUser.getUserPassword())) {
            dto.setSuccess("false");
            dto.setMsg("密码错误");

        } else {
            dto.setSuccess("false");
            dto.setMsg("账号错误");
        }
        return dto;
    }

    /**
     * 用户注销
     *
     * @param token
     * @return
     */
    @Override
    public Dto loginOut(String token) {
        Dto dto = new Dto();
        if (tokenUtil.clean(token)) {
            dto.setSuccess("true");
            dto.setErrorCode("0");
        } else {
            dto.setSuccess("false");
            dto.setMsg("token无效，该用户没有登陆过");
        }
        return dto;
    }

    @Override
    public String getVerificationCode(String code) {
        System.out.println((String) redisUtil.get(code));
        return (String) redisUtil.get(code);
    }

    @Override
    public boolean findUserCode(ItripUser itripUser) {
        if (itripUserMapper.findUserCode(itripUser) == null) {
            return true;
        }
        return false;
    }

    @Override
    public boolean insert(ItripUser record) {
        if (itripUserMapper.insert(record) >= 1) {
            return true;
        }
        return false;
    }

    @Override
    public ItripUser findUserById(long id) {
        return itripUserMapper.findUserById(id);
    }

    @Override
    public int updateActivated(int activated, long idInt) {
        return itripUserMapper.updateActivated(activated, idInt);
    }

    @Override
    public ItripUser selectUserid(String userCode) {
        return itripUserMapper.selectUserid(userCode);
    }

    @Override
    public void addVerificationCode(String code, String order) {
        redisUtil.set(code, order, 6000);
    }

    @Override
    public Dto retoken(String token) {
        Dto dto = new Dto<>();
        if (redisUtil.hasKey(token)) {
            ItripUser itripUser = (ItripUser) redisUtil.get(token);
            redisUtil.del(new String[]{token});
            String key = tokenUtil.createToken(itripUser);

            dto.setSuccess("true");
            dto.setErrorCode("0");
            TokenVO tokenVO = new TokenVO();
            tokenVO.setExpTime(36000L);
            tokenVO.setToken(key);
            dto.setData(tokenVO);
            redisUtil.set(key, itripUser, 36000L);
        } else {
            dto.setSuccess("false");
            dto.setMsg("token不存在");
        }
        return dto;
    }
}
